import request from '@/utils/request';

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data 
 * @returns
 */
export function login (data) {
	const params = {
		...data,
		clientId: data.clientId || clientId,
		grantType: data.grantType || 'password'
	};
	return request({
		url: '/auth/login',
		headers: {
			isToken: false,
			isEncrypt: true,
			repeatSubmit: false
		},
		method: 'post',
		data: params
	});
}

// 注册方法
export function register (data) {
	const params = {
		...data,
		clientId: clientId,
		grantType: 'password'
	};
	return request({
		url: '/auth/register',
		headers: {
			isToken: false,
			isEncrypt: true,
			repeatSubmit: false
		},
		method: 'post',
		data: params
	});
}

/**
 * 注销
 */
export function logout () {
	if (import.meta.env.VITE_APP_SSE === 'true') {
		request({
			url: '/resource/sse/close',
			method: 'get'
		});
	}
	return request({
		url: '/auth/logout',
		method: 'post'
	});
}

/**
 * 获取验证码
 */
export function getCodeImg () {
	return request({
		url: '/auth/code',
		headers: {
			isToken: false
		},
		method: 'get',
		timeout: 20000
	});
}

/**
 * 第三方登录
 */
export function callback (data) {
	const LoginData = {
		...data,
		clientId: clientId,
		grantType: 'social'
	};
	return request({
		url: '/auth/social/callback',
		method: 'post',
		data: LoginData
	});
}

// 获取用户详细信息
export function getInfo () {
	return request({
		url: '/system/user/getInfo',
		method: 'get'
	});
}

// 获取租户列表
export function getTenantList (isToken) {
	return request({
		url: '/auth/tenant/list',
		headers: {
			isToken: isToken
		},
		method: 'get'
	});
}
